<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/25
 * Time: 下午4:30
 */

namespace app\admin\model;


use think\Db;

class Adminlog  extends Base
{
    protected $table = "admin_log";
    protected $base_arr = [
        'table' => 'admin_log',
        'field' => 'id',
        'primary_key' => 'id'
    ];

    protected $attach_arr =  [


    ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => $this->table,//必填
            'model' => '日志',//必填
            'delmonth' => '{"1":"过去一周","2":"过去一个月","3":"过去三个月","4":"过去一个月"}',
        ];
        return $data;
    }


    /**
     * 获取日志的访问方法
     **/
    public static function getAdminLog($list_action='',$form_data=[],$userId,$admin_type){
        $model = new Adminlog();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';
        switch($actionName){
            //列表
            case "index":
                Base::save_admin_log($userId, $model, 'check','',[],[],$admin_type);
                break;
            case "del_log":
                $form_data = $model->request->param();
                switch ($form_data['delmonth']) {
                    case  '1':
                        $str = 'DATE_SUB(CURDATE(),INTERVAL 1 WEEK) <= date(create_time)';

                        break;
                    case '2':
                        $str = 'DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(create_time)';

                        break;
                    case '3':
                        $str = 'DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(create_time)';

                        break;
                    case '4':
                        $str = 'DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(create_time)';

                        break;

                }
                Db::name('admin_log')->where($str)->update(['is_deleted'=>3,'opera_id'=>$userId]);

                Base::save_admin_log($userId, $model, 'del','',$form_data,[],$admin_type);
                break;
        }

        $result = true;

        if($model->request->isPost()){

            //$model 实例化之后的模型
            //$type 数据的操作类型
            //$form_data  form表单提交之后的表单
            //$url 跳转路径
            //$old_data 原有的数据
            //$attach_arr 附属表单数组
            //$primary_key 基本表单的主键值id
            if ($type!='clear'){
                $result =  Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);

            }
        }
        else if($type == 'detail'){

            $result =  Base::operaTable($model,$userId,$type,$form_data,'',$old_data,$model->attach_arr,'id',$admin_type,false);
        }

        return $result;


    }
}